<?php 
require_once ("Login/config.php");
include_once 'Login/databaseconnection.php';
if ($_FILES["image"]["error"] > 0)
  {
 $_SESSION['errorimgexi']="Please select the Photo!";
  }
  else if($_FILES['image']['size'] == 0 || $_FILES['image']['size'] > 200*1024)
{
$_SESSION['errorimgexi']="The photo size must be less than 200kb";
}
else if(file_exists("img/" . $_FILES["image"]["name"]))
      {
     $_SESSION['errorimgexi']=$_FILES["image"]["name"] . " already exists.". "<br />";
      }
    else
      {
	  move_uploaded_file($_FILES["image"]["tmp_name"],"img/" . $_FILES["image"]["name"]);
	  if(file_exists("img/" . $_FILES["image"]["name"]))
      {
	  date_default_timezone_set("Europe/Paris");
$t_d=date("Y-m-d");
$getphid=mysql_query("SELECT MAX(Photo_Id) AS Max_P_Id FROM photos");
if($g_p_id=mysql_fetch_array($getphid))
{
$Max_Id=$g_p_id['Max_P_Id'];
if($Max_Id=="")
{
$M_Id="PH0001";
}
else
{
$inum=substr("$Max_Id",2);
$inn=($inum+1);
$r=(strlen($inum)-strlen($inn));
$M_Id='PH';
if(strlen($inn)<strlen($inum))
{
for($i=0;$i<(strlen($inum)-strlen($inn));$i++)
{
$M_Id=$M_Id.'0';
}
$M_Id=$M_Id.$inn;
}
else
{
$M_Id=$M_Id.$inn;
}
}
}
$img=$_FILES["image"]["name"];
$Added_By=$_SESSION['username'];
if(strlen($img)>50)
{
unlink("img/$img"); 
$_SESSION['errorimgexi']="The name of the photo is too long!";
}
if(strlen($img)<=50)
{
$add_img=mysql_query("INSERT INTO photos(Photo_Id,Photo,Added_By,Added_Date) VALUES('$M_Id','$img','$Added_By','$t_d')");
if($add_img)
{
$_SESSION['errorimgsucc']=$_FILES["image"]["name"]. " is successfully stored. <br />";
}
if(!$add_img)
{
unlink("img/$img"); 
$_SESSION['errorimgexi']="Unable to store ".$_FILES["image"]["name"]. ".<br />";
}
}
}
else
{
$_SESSION['errorimgexi']="Unable to store ".$_FILES["image"]["name"]. ".<br />";
}
}
?>